﻿using System.Data.Entity;
using CodeFirst.Models;
using CodeFirst.Models.Mapping;

namespace CodeFirst
{
    using System;
    using System.Data.Entity;
    using System.Linq;

    public class CodeFirstContext : DbContext
    {
        //启用数据迁移 Enable-Migrations
        //添加迁移版本 Add-Migration <版本号>
        //应用数据迁移 Update-Database
        public CodeFirstContext()
            : base("name=CodeFirstContext")
        {
            //设置自动迁移
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<CodeFirstContext, Migrations.Configuration>());
        }

        public virtual DbSet<Grade> Grades { get; set; }
        public virtual DbSet<Student> Students { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //注册复杂类型配置
            modelBuilder.Configurations.Add(new GradeMap());
            modelBuilder.Configurations.Add(new StudentMap());
        }
    }
}